Scheduler for UWP
予定の連結
C1Scheduler の使い方 > データ連結 > 予定の連結

Scheduler for UWP では、カスタムデータソースに連結できます。NestedPropertySetter を使用して、AppointmentStorage.DataSource プロパティ設定することで、予定のプロパティと AppointmentStorage クラスの間のマッピングを設定します。

適切な参照と C1Scheduler コントロールが追加されているプロジェクトから始めます。

初期の名前空間宣言と XAML マークアップは、次のサンプルのようになります。

XAML
コードのコピー
<Page
    x:Class="ScheduleSamples.Samples.BusinessObjectsBinding"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ScheduleSamples.Samples"
    xmlns:Schedule="using:C1.Xaml.Schedule"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Schedule:C1Scheduler x:Name="sched1"  >            
        </Schedule:C1Scheduler>
    </Grid>
</Page>

ページを右クリックし、コンテキストメニューから[コードの表示]を選択します。コードファイルが開いたら、名前空間宣言をチェックして、次のようになっていることを確認します。

C#
コードのコピー
using C1.C1Schedule;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Serialization;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

次に、MainPage() コンストラクタを編集して、AppointmentCollection: にデモ用の予定を追加します。

C#
コードのコピー

     public MainPage()
     {
         this.InitializeComponent();
         sched1.Settings.FirstVisibleTime = System.TimeSpan.FromHours(8);

         AppointmentCollection apps = Resources["_ds"] as AppointmentCollection;
         if (apps != null)
         {
             // デモ用の予定を追加します
             Appointment app = new Appointment();
             app.Subject = "test appointment";
             app.Start = DateTime.Today;
             apps.Add(app);
         }
     }
 }

次に、カスタム Appointment クラスと PropertyChangedEventHandler を追加します。

C# でコードを書く場合

C#
コードのコピー

[DataContract(Name = "Appointment", Namespace = "http://www.componentone.com")]
public class Appointment : INotifyPropertyChanged
{
    public Appointment()
    {
        Id = Guid.NewGuid();
    }
    [DataMember]
    public Guid Id { get; private set; }

    private string _subject = "";
    [DataMember]
    public string Subject
    {
        get { return _subject; }
        set
        {
            if (_subject != value)
            {
                _subject = value;
                OnPropertyChanged("Subject");
            }
        }
    }

    private string _location = "";
    [DataMember]
    public string Location
    {
        get { return _location; }
        set
        {
            if (_location != value)
            {
                _location = value;
                OnPropertyChanged("Location");
            }
        }
    }

    private DateTime _start;
    [DataMember]
    public DateTime Start
    {
        get { return _start; }
        set
        {
            if (_start != value)
            {
                _start = value;
                OnPropertyChanged("Start");
            }
        }
    }

    [DataMember]
    public DateTime End
    {
        get { return _start.Add(_duration); }
        set
        {
            if (value >= _start)
            {
                Duration = (value.Subtract(_start));
                OnPropertyChanged("End");
            }
        }
    }

    private TimeSpan _duration;
    public TimeSpan Duration
    {
        get { return _duration; }
        set
        {
            if (_duration != value)
            {
                _duration = value;
                OnPropertyChanged("Duration");
            }
        }
    }

    private string _description = "";
    [DataMember]
    public string Description
    {
        get { return _description; }
        set
        {
            if (_description != value)
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }
    }

    private string _properties = "";
    [DataMember]
    public string Properties
    {
        get { return _properties; }
        set
        {
            if (_properties != value)
            {
                _properties = value;
                OnPropertyChanged("Properties");
            }
        }
    public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

ObservableCollection を継承する AppointmentCollection クラスを追加します。

C#
コードのコピー
public class AppointmentCollection : ObservableCollection<Appointment>
{
    public AppointmentCollection()
    {
    }
}

次に、MainPage.xaml ページでカスタムデータソースのインスタンスを作成します。ローカルリソースの Key 値に、先にコードで定義した名前を設定します。

XAML
コードのコピー
<Page.Resources>
    <local:AppointmentCollection x:Key="_ds"/>
</Page.Resources>

NestedPropertySetter を使用して、Appointment クラスで設定された予定のプロパティと AppointmentStorage クラスの間のマッピングを設定します。

XAML
コードのコピー
<c1:C1Scheduler x:Name="sched1" >
    <!-- AppointmentStorage をビジネスオブジェクトのコレクションにマップします -->
    <c1:NestedPropertySetter
        PropertyName="DataStorage.AppointmentStorage.Mappings.AppointmentProperties.MappingName"
        Value="Properties"/>
    <c1:NestedPropertySetter
        PropertyName="DataStorage.AppointmentStorage.Mappings.Body.MappingName"
        Value="Description"/>
    <c1:NestedPropertySetter
        PropertyName="DataStorage.AppointmentStorage.Mappings.End.MappingName"
        Value="End"/>
    <c1:NestedPropertySetter
        PropertyName="DataStorage.AppointmentStorage.Mappings.IdMapping.MappingName"
        Value="Id"/>
    <c1:NestedPropertySetter
        PropertyName="DataStorage.AppointmentStorage.Mappings.Location.MappingName"
        Value="Location"/>
    <c1:NestedPropertySetter
        PropertyName="DataStorage.AppointmentStorage.Mappings.Start.MappingName"
        Value="Start"/>
    <c1:NestedPropertySetter
        PropertyName="DataStorage.AppointmentStorage.Mappings.Subject.MappingName"
        Value="Subject"/>
    <c1:NestedPropertySetter PropertyName="DataStorage.AppointmentStorage.DataSource"
        Value="{Binding Mode=TwoWay, Source={StaticResource _ds}}" />
</c1:C1Scheduler>

アプリケーションを実行すると、現在の日にマークが付いたテスト予定が表示されます。